﻿using System.Collections.Generic;
using System.Linq;



namespace Kernelx.Contexts.ClientImpls
{
    using Kernelx.Contexts.Core;
    using Kernelx.Extensions;

    internal static class RequsetParameter
    {
        protected class RequsetParameterDict : RequestParameterCollectionBase
        {
            public RequsetParameterDict(IDictionary<string, object> parameters) : base(() => parameters) { }
        }

        protected class RequsetParameterObject : RequestParameterCollectionBase
        {
            public RequsetParameterObject(object parameters) : base(() => Convert(parameters)) { }

            private static IDictionary<string, object> Convert(object parameters)
            {
                var type = parameters.GetType();
                var props = type.GetProperties().Where(x => x.CanRead).ToList();
                return props.ToDictionary(kv => kv.Name, kv => kv.GetValue(parameters, kv.GetIndexValues()));
            }
        }

        public static IRequestParameterCollection Create(object parameters)
            => parameters is IDictionary<string, object> ? new RequsetParameterDict(parameters as IDictionary<string, object>) :
            (IRequestParameterCollection)new RequsetParameterObject(parameters);
    }
}
